//
//  PAMediaAsynManager.m
//  PAMobileWallet
//
//  Created by Gavin Zhang on 7/26/13.
//  Copyright (c) 2013 平安付. All rights reserved.
//

#import "PAMediaAsynManager.h"
#import "PAMediaRequest.h"

#import "VoiceConverter.h"
static PAMediaAsynManager *_sharedInstance;

@implementation PAMediaAsynManager


#pragma mark - singleton
+(PAMediaAsynManager *)sharedInstance
{
    if (!_sharedInstance) {
        _sharedInstance = [[super allocWithZone:NULL] init];
    }
    return _sharedInstance;
}
+(id)allocWithZone:(NSZone *)zone
{
    return [PAMediaAsynManager sharedInstance];
}
-(id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (void)loadMediaWithRequest:(PAMediaRequest *)req
{
    req.delegate = self;
    req.bufferData = [NSMutableData data];
    NSString *json = nil;
//	[req addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
    [req addRequestHeader:@"Content-Type" value:@"application/octet-stream"];

	[req appendPostData:[json dataUsingEncoding:NSUTF8StringEncoding]];
	[req startAsynchronous];
}

#pragma mark - ASIHTTPRequestDelegate implementation
- (void)requestStarted:(ASIHTTPRequest *)request
{
    if ([request isKindOfClass:[PAMediaRequest class]]){
        PAMediaRequest *req = (PAMediaRequest *)request;
        if (req.paMediaAsynDelegate && [req.paMediaAsynDelegate respondsToSelector:@selector(paMediaRequestStarted)]) {
            [req.paMediaAsynDelegate paMediaRequestStarted];
        }
    }
    
}
//- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
//{}
//- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL
//{}
- (void)requestFinished:(ASIHTTPRequest *)request
{
    if ([request isKindOfClass:[PAMediaRequest class]]) {
        PAMediaRequest *req = (PAMediaRequest *)request;
        
        NSError *error = nil;
        NSString *filePath = [DocumentsPath stringByAppendingPathComponent:req.uniqueId];
        NSString *amrPath = [filePath stringByAppendingPathExtension:@"amr"];
        NSString *wavPath = [filePath stringByAppendingPathExtension:@"wav"];
        BOOL isSaved = [req.bufferData writeToFile:amrPath options:NSDataWritingAtomic error:&error];
        if (isSaved) {
            NSLog(@"交易-%@ 音频文件amr 写入成功",req.uniqueId);
        }else{
            NSLog(@"交易-%@ 音频文件amr 写入失败",req.uniqueId);
        }
        isSaved = [VoiceConverter  amrToWav:amrPath wavSavePath:wavPath];
        if (isSaved) {
            NSLog(@"交易-%@ 音频文件wav 转码成功",req.uniqueId);
        }else{
            NSLog(@"交易-%@ 音频文件wav 转码失败",req.uniqueId);
        }
//        [[NSFileManager defaultManager] removeItemAtPath:[filePath stringByAppendingPathExtension:@"amr"] error:&error];
        
        if (req.paMediaAsynDelegate && [req.paMediaAsynDelegate respondsToSelector:@selector(paMediaRequestSuccess:)]) {
            [req.paMediaAsynDelegate paMediaRequestSuccess:wavPath];
        }
    }
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"image requestFailed\n%@",request.error);
    if ([request isKindOfClass:[PAMediaRequest class]]) {
        PAMediaRequest *req = (PAMediaRequest *)request;
        if (req.paMediaAsynDelegate && [req.paMediaAsynDelegate respondsToSelector:@selector(paMediaRequestFailed)]) {
            [req.paMediaAsynDelegate paMediaRequestFailed];
        }
    }
}
//- (void)requestRedirected:(ASIHTTPRequest *)request
//{}
- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
{
    if ([request isKindOfClass:[PAMediaRequest class]]) {
        PAMediaRequest *req = (PAMediaRequest *)request;
        [req.bufferData appendData:data];
    }
}
//- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request
//{}
//- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request
//{}


@end
